#! python
# -*- coding: utf-8 -*-
"""
@File Name: 计算器_calculator_plus_ext.py
@Author: LT
@Date: 2021/10/27 14:48
# -----------------------------------------------------------------------------------------------------
@Description: 
在容器里额外添加一行按钮，用于常用操作；一个更有用的定制：通过创建子类而非嵌入的方式添加按钮，
用于更多操作（平方根、倒数等）；按钮放在帧之前，而帧附加到calgui类的底部。
# -----------------------------------------------------------------------------------------------------
"""
from tkinter import *
import sys, os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
__package__ = os.path.split(os.path.dirname(__file__))[-1]
from .计算器_calculator import CalcGui, getCalcArgs
from .涉及tkinter的包.组件构建器_widgets import label, frame, button


class CalcGuiPlus(CalcGui):
    def makeWidgets(self, *args):
        label(self, TOP, 'PyCalc Plus - Subclass')
        CalcGui.makeWidgets(self, *args)
        frm = frame(self, BOTTOM)
        extras = [('sqrt', 'sqrt(%s)'),
                  ('x^2 ', '(%s)**2'),
                  ('x^3 ', '(%s)**3'),
                  ('1/x ', '1.0/(%s)')]
        for (lab, expr) in extras:
            button(frm, LEFT, lab, (lambda expr=expr: self.onExtra(expr)))
        button(frm, LEFT, ' pi ', self.onPi)

    def onExtra(self, expr):
        try:
            self.text.set(self.eval.runstring(expr % self.text.get()))
        except:
            self.text.set('ERROR')

    def onPi(self):
        self.text.set(self.eval.runstring('pi'))


if __name__ == '__main__':
    CalcGuiPlus(**getCalcArgs()).mainloop()       # passes -bg, -fg on
